﻿Imports Phila
Imports Phila.Sql
Imports Phila.CommonClasses

Public Class EditTestimonial
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            If Not IsPostBack = True Then
                If Not Request.QueryString("id") Is Nothing Then
                    fillData(Request.QueryString("id"))
                End If
            End If
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Private Sub fillData(ByVal Id As Integer)
        Dim arr As Array
        arr = RetriveData("SELECT [FromName] ,[Date] ,[Message], Photo, IsActive FROM ptn_Testimonials where Id=" & Id, 4)
        txtName.Text = arr(0)
        txtDate.Text = arr(1)
        txtMessage.Content = arr(2)
        ltrImg.Text = "<img src=""../images/img/" & arr(3) & """ align=""absmiddle"" />"
        chkActive.Checked = arr(4)
    End Sub

    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Try
            Dim intId As Integer
            Dim sDate As DateTime
            Dim strActive As String
            'Dim chkActive As String
            If chkActive.Checked = True Then
                strActive = "1"
            Else
                strActive = "0"
            End If
            sDate = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", Nothing)
            If Request.QueryString("id") Is Nothing Then
                Insert_Update_Delete_Record("INSERT INTO ptn_Testimonials ([FromName] ,[Date] ,[Message],[IsActive]) VALUES ('" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "','" & Server.HtmlEncode(txtDate.Text.Replace("'", "&#039;")) & "' ,'" & Server.HtmlEncode(txtMessage.Content.Replace("'", "&#039;")) & "'," & strActive & ")")
                intId = Request.QueryString("id")
            Else
                Insert_Update_Delete_Record("Update ptn_Testimonials set [FromName]='" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "',[Date]='" & sDate & "' ,[Message]='" & Server.HtmlEncode(txtMessage.Content.Replace("'", "&#039;")) & "', IsActive=" & strActive & " where Id=" & Request.QueryString("Id"))
                Dim arr As Array
                arr = RetriveData("Select top 1 Id from ptn_Testimonials where FromName='" & Server.HtmlEncode(txtName.Text.Replace("'", "&#039;")) & "' and Date='" & sDate & "' order by Id desc", 0)
                intId = arr(0)
            End If

            UploadFile("ptn_Testimonials", "Photo", fileUpload, intId, "/img")

            Response.Redirect("ViewTestimonials.aspx")
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Protected Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Response.Redirect("ViewTestimonials.aspx")
    End Sub

End Class